#ifndef ELTOOLTIP_H
#define ELTOOLTIP_H

#include "elcomponent.h"

class QPropertyAnimation;
class QEnterEvent;

class ElTooltip : public ElComponent
{
    friend void ElComponent::setTooltip(ElTooltip *tooltip);

public:
    enum Effect { Dark, Light };
    enum Placement {
        Top,
        TopStart,
        TopEnd,
        Bottom,
        BottomStart,
        BottomEnd,
        Left,
        LeftStart,
        LeftEnd,
        Right,
        RightStart,
        RightEnd
    };

    ///
    /// \brief ElTooltip
    /// must be careful that ElTooltip's parent should be a top level widget.
    /// Because Qt will paint it inside the parent widget.
    /// Its moving and geometry logic have been encapsulated internally.
    ///
    ElTooltip(ElComponent *target = nullptr, const QString &content = QString());

    ElComponent *target() const;
    // when calling ElComponent's setTooltip, this function will be called internally.
    void setTarget(ElComponent *target);

    Effect effect() const;
    void setEffect(Effect effect);

    QString content() const;
    void setContent(const QString &content);

    Placement placement() const;
    void setPlacement(Placement placement);

    qreal offset() const;
    void setOffset(qreal offset);

    bool isArrowVisible() const;
    void setArrowVisible(bool b);

    int openDelay() const;
    void setOpenDelay(int openDelay);

    bool isManual() const;
    void setManual(bool b);

    bool isEnterable() const;
    void setEnterable(bool b);

    int hideAfter() const;
    void setHideAfter(int hideAfter);

public slots:
    void fade();

protected:
    ElComponent *_target = nullptr;
    Effect _effect = Dark;
    QString _content;
    Placement _placement = Bottom;
    qreal _offset = 0;
    bool _arrowVisible = true; // TODO: draw arrow
    int _openDelay = 0;
    bool _manual = false;
    bool _enterable = true;
    int _hideAfter = 0; // 0 represents of not hiding automatically.

    QPropertyAnimation *animation = nullptr;

    bool event(QEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    void showEvent(QShowEvent *e) override;
    void enterEvent(QEvent *e) override;
    void leaveEvent(QEvent *e) override;
    void initPalette() override;

private:
    using ElComponent::setTooltip;

    Q_PROPERTY(ElComponent *target READ target WRITE setTarget)
    Q_PROPERTY(Effect effect READ effect WRITE setEffect)
    Q_PROPERTY(QString content READ content WRITE setContent)
    Q_PROPERTY(Placement placement READ placement WRITE setPlacement)
    Q_PROPERTY(qreal offset READ offset WRITE setOffset)
    Q_PROPERTY(bool arrowVisible READ isArrowVisible WRITE setArrowVisible)
    Q_PROPERTY(int openDelay READ openDelay WRITE setOpenDelay)
    Q_PROPERTY(bool manual READ isManual WRITE setManual)
    Q_PROPERTY(bool enterable READ isEnterable WRITE setEnterable)
    Q_PROPERTY(int hideAfter READ hideAfter WRITE setHideAfter)

    static const qreal Padding_Lr;
    static const qreal Padding_Tb;
    static const qreal Spacing;
    static const qreal Triangle_H;

    bool isEntered = false;
    QTimer *timer = nullptr;

    void initAnimation();
    void initTimer();
    void movePos();   // update position by replacement
    void updatePos(); // update position since the target may have moved
};

#endif // ELTOOLTIP_H
